﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Collections;

public partial class CardCenter_CardTransfer : System.Web.UI.Page
{
    public int subCardNo = 0;
    WebUtility util = new WebUtility();
    /// <summary>
    /// 获取隐藏的卡序号
    /// </summary>
    public string GetHideCardValue
    {
        get { return this.hideCardValue.Value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    //转账
    protected void ImgBtnTrans_Click(object sender, ImageClickEventArgs e)
    {
        string subCards = this.hideSubcardList.Value.ToString();

        //存错误信息列表
        List<string> errorList = new List<string>();
        //解析格式：78956-1345|789456-9822|12456-456123|
        string[] cardNo2Pwd = subCards.Trim().Split('|');
        int k = cardNo2Pwd.Length;
        Dictionary<string, string> card2pwdDic = new Dictionary<string, string>();
        //获取用户输入的子卡列表
        for (int z = 0; z < k;z++ )
        {
            string[] pair = cardNo2Pwd[z].Split('-');
            if(!string.IsNullOrEmpty(pair[0]) && !string.IsNullOrEmpty(pair[1]))
            {
                if(!card2pwdDic.ContainsKey(pair[0])){
                    card2pwdDic.Add(pair[0], pair[1]);
                }
            }
        }

        //获取子卡输入的错误总数
        string totalError = this.SubValidateError.Value.ToString();

        //获取父卡卡号
        string fatherCardNo = this.txt_FatherCardNo.Text.Trim().ToString();
        string fatherCardPwd = this.txt_FatherCardPwd.Text.Trim().ToString();


        //构造一张父卡
        Cards fatherCard = new Cards();
        fatherCard.CardSeriaNumber = fatherCardNo;
        fatherCard.CardPassword = fatherCardPwd;

        if (string.IsNullOrEmpty(fatherCardNo) || string.IsNullOrEmpty(fatherCardPwd) || Convert.ToInt32(totalError) > 0)
        {
            string errorMsg = "输入信息有误，请确认！";
            util.AlertMsg(errorMsg);
        }
        else 
        {
            //构造子卡列表 
            List<Cards> subCardList = new List<Cards>();

            //遍历字典中的卡号和密码对
            Cards subCard = null;
            foreach (KeyValuePair<string, string> key2value in card2pwdDic)
            {
                string cardNo = key2value.Key;
                string cardPwd = key2value.Value;
                subCard = new Cards();
                subCard.CardSeriaNumber = cardNo;
                subCard.CardPassword = cardPwd;
                subCardList.Add(subCard);
            }
            //---------------------------------
            // 显示结果
            BLL.CardsPointsTransfer transfer = new CardsPointsTransfer();
            errorList = transfer.Many2OneMergeCards(fatherCard, subCardList);

            Session["errorList"] = errorList;
            Response.Redirect("CardTransferResult.aspx");
        }
        
    }
}
